package com.zhang.servicemap.remote;

import com.zhang.internalcommon.constant.AmapConfigConstant;
import com.zhang.internalcommon.dto.ResponseResult;
import com.zhang.internalcommon.response.TerminalResponse;
import com.zhang.internalcommon.response.TrsearchResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * 类功能描述
 *
 * @author Kye
 * @date 2025-04-15
 */
@Service
public class TerminalClient {
	@Value("${amap.key}")
	private String amapKey;

	@Value("${amap.sid}")
	private String amapSid;

	@Autowired
	RestTemplate restTemplate;

	public ResponseResult<TerminalResponse> add(String name, String desc) {
		// 拼装请求的url
		StringBuilder url = new StringBuilder();
		url.append(AmapConfigConstant.TERMINAL_ADD_URL);
		url.append("?");
		url.append("key=" + amapKey);
		url.append("&sid=" + amapSid);
		url.append("&name=" + name);
		url.append("&desc=" + desc);

		ResponseEntity<String> forEntity = restTemplate.postForEntity(url.toString(), "", String.class);
		String body = forEntity.getBody();
		JSONObject result = JSONObject.fromObject(body);
		JSONObject data = result.getJSONObject("data");
		String tid = data.getString("tid");

		TerminalResponse terminalResponse = new TerminalResponse();
		terminalResponse.setTid(tid);

		return ResponseResult.success(terminalResponse);
	}

	public ResponseResult<List<TerminalResponse>> aroundSearch(String center, Integer radius) {
		// 拼装请求的url
		StringBuilder url = new StringBuilder();
		url.append(AmapConfigConstant.TERMINAL_AROUNDSEARCH_URL);
		url.append("?");
		url.append("key=" + amapKey);
		url.append("&sid=" + amapSid);
		url.append("&center=" + center);
		url.append("&radius=" + radius);

		System.out.println("终端搜索请求：" + url.toString());
		ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url.toString(), null, String.class);
		System.out.println("终端搜索响应；" + stringResponseEntity.getBody());

		String body = stringResponseEntity.getBody();
		JSONObject result = JSONObject.fromObject(body);
		JSONObject data = result.getJSONObject("data");

		List<TerminalResponse> terminalResponseList = new ArrayList<>();

		JSONArray results = data.getJSONArray("results");
		for (int i = 0; i < results.size(); i++) {
			TerminalResponse terminalResponse = new TerminalResponse();

			JSONObject jsonObject = results.getJSONObject(i);
			String desc = jsonObject.getString("desc");
			long carId = Long.parseLong(desc);
			String tid = jsonObject.getString("tid");

			JSONObject location = jsonObject.getJSONObject("location");
			String longitude = location.getString("longitude");
			String latitude = location.getString("latitude");

			terminalResponse.setCarId(carId);
			terminalResponse.setTid(tid);
			terminalResponse.setLongitude(longitude);
			terminalResponse.setLatitude(latitude);

			terminalResponseList.add(terminalResponse);
		}

		return ResponseResult.success(terminalResponseList);
	}

	public ResponseResult<TrsearchResponse> trsearch(String tid, Long starttime, Long endtime) {
		// 拼装请求的url
		StringBuilder url = new StringBuilder();
		url.append(AmapConfigConstant.TERMINAL_SEARCH_URL);
		url.append("?");
		url.append("key=" + amapKey);
		url.append("&sid=" + amapSid);
		url.append("&tid=" + tid);
		url.append("&starttime=" + starttime);
		url.append("&endtime=" + endtime);

		System.out.println("轨迹搜索请求：" + url.toString());
		ResponseEntity<String> forEntity = restTemplate.getForEntity(url.toString(), String.class);
		System.out.println("轨迹搜索响应；" + forEntity.getBody());

		JSONObject jsonObject = JSONObject.fromObject(forEntity.getBody());
		JSONObject data = jsonObject.getJSONObject("data");
		int counts = data.getInt("counts");
		if (counts == 0) {
			return null;
		}
		JSONArray tracks = data.getJSONArray("tracks");
		long driveMile = 0L;
		long driveTime = 0L;
		for (int i = 0; i < tracks.size(); i++) {
			JSONObject jsonObject1 = tracks.getJSONObject(i);
			long distance = jsonObject1.getLong("distance");
			driveMile = driveMile+distance;

			long time = jsonObject1.getLong("time");
			time = time/(1000*60);
			driveTime = driveTime + time;
		}
		TrsearchResponse trsearchResponse = new TrsearchResponse();
		trsearchResponse.setDriveMile(driveMile);
		trsearchResponse.setDriveTime(driveTime);
		return ResponseResult.success(trsearchResponse);
	}
}
